<?php
namespace app\admin\model;
use think\Loader;
use think\Model;
use think\Validate;

class CateModel extends Model
{
    protected $pk = 'id';//主键
    //设置当前模型对应的完整数据表名称
    protected $table = 'fly_category';
    //后台用户中心数据

    //获取分类
    public function cateList()
    {
    	$cateList = $this->select();
    	// dump($userList);die;
    	return $cateList;
    }

    //添加分类
    public function add($data)
    {
        $validate = new Validate([
            'category_name'  => 'require'
        ],[
            'category_name.require'  => '不能为空'
            ]
    );
        if (!$validate->check($data)) {
            return ['valid'=> 0,'msg'=>$validate->getError()];
        }
        
        $res = $this->allowField(true)->save($data);

        if($res){
            return ['valid'=>1, 'msg'=>'添加成功'];
        }else{
            return ['valid'=>0, 'msg'=>'添加失败'];

        }
    }
    public function cateEdit($data)
    {
        $cateInfo = $this->where('id', $data)->find();
        return $cateInfo->data;
    }
    public function cateEditDo($data)
    {
         //执行验证
        $validate = new Validate([
            'category_name'  => 'require'
        ],[
            'category_name.require'  => '请输入分类名'
            ]
    );
        if (!$validate->check($data)) {
            return ['valid'=> 0,'msg'=>$validate->getError()];
        }
        
        //修改分类
        // save方法第二个参数为更新条件
        $res = $this->save([
            'category_name'  =>$data['category_name'],
        ],[$this->pk => $data['id']]);
        if($res){
            return ['valid'=>1, 'msg'=>'分类修改成功'];
        }else{
            return ['valid'=>0, 'msg'=>'分类修改失败'];

        }


    }
   


}


